﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApiV1.Date;
using WebApiV1.Date.Entity;
using WebApiV1.Domain;

namespace WebApiV1.Utils
{
    public class DbInitializer
    {

        public static void Seed(IServiceProvider serviceProvider)
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Admin1000Context)) as Admin1000Context;

                var _user = scope.ServiceProvider.GetService(typeof(IRespository<Users>)) as EFResPository<Users>;

                var _role = scope.ServiceProvider.GetService(typeof(IRespository<Roles>)) as EFResPository<Roles>;

                db.Database.EnsureCreated();

                var hasUser = db.Users.Any();
                if (!hasUser)
                {
                    var role = new Roles()
                    {
                        RolesName = "豌豆射手"
                    };
                    _role.Insert(role);

                    var user = new Users()
                    {
                        UserName = "爆炸樱桃",
                        PassWord = "123456",
                        Roles = role
                    };

                    _user.Insert(user);

                    // db.SaveChanges();
                }

            }

        }

        public static void Seed()
        {
            throw new NotImplementedException();
        }

        public static void seed(IServiceProvider applicationServices)
        {
            throw new NotImplementedException();
        }
    }
}
